package com.example.leetdemo.plan1;

public class Sword001 {

    public int divide(int a, int b) {
        if (b == 0) {
            return (1 << 31) - 1;
        }
        int flag = 0;
        if ((a >= 0 && b < 0) || (a < 0 && b >=0))  {
            flag = 1;
        }
        String al = Integer.toBinaryString(Math.abs(a)), bl = Integer.toBinaryString(Math.abs(b));
        if(al.length() < bl.length()) {
            return 0;
        }

        String str = 0 + al.substring(0, al.length() - bl.length());
        int result = flag == 1 ? -Integer.parseInt(str, 2) : Integer.parseInt(str, 2);
        if (al.substring(al.length()  - bl.length()).compareTo(bl) >= 0) {
            return flag == 0 ? result + 2 : result - 2;
        } else {
            return result;
        }


    }

    public static void main(String[] args) {
        Sword001 sword001 = new Sword001();
        System.err.println(sword001.divide(15, -2));
    }

}
